/*-----------------------------------------------------------------------------
 * file:  sr_arp.h 
 * date:  Thu Oct 18, 2012  
 * Author: yujiali@cs.toronto.edu
 *
 * Description:
 *
 * Methods and datastructures for handeling the ARP packets 
 *---------------------------------------------------------------------------*/

#ifndef sr_ARP_H
#define sr_ARP_H

#include <netinet/in.h>
#include <pthread.h>

#include "sr_protocol.h"

struct sr_instance;
struct sr_if;

#define ARP_ENTRY_INIT_TTL  15
#define ARP_REQUEST_TIMEOUT 5

#define ARP_PKT_BUF_LEN 256

#define THREAD_POOL_SIZE    255

struct sr_at 
{
    uint32_t      ip;   /* IP address */
    uint8_t       ether_addr[ETHER_ADDR_LEN];   /* Ethernet address */
    uint8_t       ttl;  /* TTL for timeout */
    struct sr_at* next;
};

struct sr_arp_q
{
    uint8_t*         pkt;   /* packet buffer (complete with Ethernet headers) */
    uint32_t         ip_gw; /* gateway IP that this packet is requesting */
    struct sr_if*    iface; /* pointer to the outgoing interface */
    uint8_t          ttl;   /* TTL for timeout */
    struct sr_arp_q* next;
};

int sr_handle_arp(struct sr_instance*, uint8_t*, unsigned int, char*);
int sr_search_arp(struct sr_instance*, uint32_t, uint8_t*);
int sr_request_arp(struct sr_instance*, uint32_t, struct sr_if*);

void *sr_check_arp_entries(void* sr);
void *sr_check_arp_queue(void* sr);
int sr_add_to_queue(struct sr_instance* sr, uint32_t ip_gw, struct sr_if* iface, 
        uint8_t* ip_hdr, unsigned int ip_len);


#endif /* -- sr_ARP_H --*/
